package com.springboot.controller;

import com.springboot.dto.ChatList;
import com.springboot.model.ChatRecord;
import com.springboot.model.UserLoginForm;
import com.springboot.service.DoctorChattingListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 * 本控制台用于医生在线咨询显示聊天列表
 */
@Controller
public class DoctorChattingListController {
    @Autowired
    DoctorChattingListService doctorChattingListService;
    /**
     * 医生查看聊天列表
     */
    @CrossOrigin
    @RequestMapping(value = "/doctorShowMessage")
    public @ResponseBody
    String doctorChattingList(@RequestParam("doctorTel") String doctorTel) {
        System.out.println("医生查看聊天列表");
        try {
            UserLoginForm doctor=doctorChattingListService.findUserByTel(doctorTel);
            int doctorId=doctor.getUserId();
            ArrayList<ChatRecord> chatRecords=doctorChattingListService.findChatByReceiverId(doctorId);
            ArrayList<ChatRecord> chatSender=doctorChattingListService.findChatBySenderId(doctorId);
            if(chatRecords.size()==0&&chatSender.size()==0)
            {
                return "空";
            }
            else
            {
                //对于该医生存储所有的发送者id
                int []allSenderId=new int[chatRecords.size()];
                for(int i=0;i<chatRecords.size();i++)
                {
                    ChatRecord chatRecord=chatRecords.get(i);
                    allSenderId[i]=chatRecord.getSenderId();
                }
                //去除重复的发送者的id
                for(int i=0;i<chatRecords.size();i++)
                {
                    for(int j=i+1;j<chatRecords.size();j++)
                    {
                        if(allSenderId[i]==allSenderId[j])
                        {
                            allSenderId[j]=0;
                        }
                    }
                }
                int []senderId=new int[chatRecords.size()];
                int senderNum=0;
                for(int i=0;i<chatRecords.size();i++)
                {
                    if(allSenderId[i]!=0)
                    {
                        senderId[senderNum]=allSenderId[i];
                        senderNum++;
                    }
                }
                //对于该医生存储所有的接收者id
                int []allReceiverId=new int[chatSender.size()];
                for(int i=0;i<chatSender.size();i++)
                {
                    ChatRecord chatRecord=chatSender.get(i);
                    allReceiverId[i]=chatRecord.getReceiverId();
                }
                for(int i=0;i<chatSender.size();i++)
                {
                    for(int j=i+1;j<chatSender.size();j++)
                    {
                        if(allReceiverId[i]==allReceiverId[j])
                        {
                            allReceiverId[j]=0;
                        }
                    }
                }
                for(int i=0;i<chatSender.size();i++)
                {
                    for(int j=0;j<chatRecords.size();j++)
                    {
                        if(allReceiverId[i]==allSenderId[j])
                        {
                            allReceiverId[i]=0;
                        }
                    }
                }
                int []receiverId=new int[chatSender.size()];
                int receiverNum=0;
                for(int i=0;i<chatSender.size();i++)
                {
                    if(allReceiverId[i]!=0)
                    {
                        receiverId[receiverNum]=allReceiverId[i];
                        receiverNum++;
                    }
                }

                for(int i=0;i<senderNum;i++)
                {
                    System.out.println("不重复的发送方"+senderId[i]);
                }

                for(int i=0;i<receiverNum;i++)
                {
                    System.out.println("不重复的接收方"+receiverId[i]);
                }

                ArrayList<ChatList> chatLists=new ArrayList<>();
                for(int i=0;i<senderNum;i++)
                {
                    //医生作为接收方
                    ChatRecord chatRecord=new ChatRecord();
                    chatRecord.setSenderId(senderId[i]);
                    chatRecord.setReceiverId(doctorId);
                    ArrayList<ChatRecord> chatRecordArrayList=doctorChattingListService.findChatByRecord(chatRecord);
                    System.out.println("发送方："+senderId[i]+": "+chatRecordArrayList.size());
                    //医生作为发送方
                    ChatRecord recordSender=new ChatRecord();
                    recordSender.setSenderId(doctorId);
                    recordSender.setReceiverId(senderId[i]);
                    ArrayList<ChatRecord> records=doctorChattingListService.findChatByRecord(recordSender);
                    System.out.println("接收方："+senderId[i]+": "+records.size());

                    String []allChat=new String[chatRecordArrayList.size()+records.size()];
                    String []allChatTime=new String[chatRecordArrayList.size()+records.size()];
                    Integer []boolPicture=new Integer[chatRecordArrayList.size()+records.size()];
                    String []allChatState=new String[chatRecordArrayList.size()+records.size()];
                    SimpleDateFormat dateTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    int chatNum=0;
                    //医生作为接收方
                    for(int j=0;j<chatRecordArrayList.size();j++)
                    {
                        ChatRecord record=chatRecordArrayList.get(j);
                        allChat[chatNum]=record.getChatContent();
                        allChatTime[chatNum]=dateTime.format(record.getChatTime());
                        boolPicture[chatNum]= record.getFlag2();
                        allChatState[chatNum]=record.getReceiverState();
                        chatNum++;
                    }
                    //医生作为发送方
                    for(int j=0;j<records.size();j++)
                    {
                        ChatRecord record=records.get(j);
                        allChat[chatNum]=record.getChatContent();
                        allChatTime[chatNum]=dateTime.format(record.getChatTime());
                        boolPicture[chatNum]= record.getFlag2();
                        allChatState[chatNum]="已读";
                        chatNum++;
                    }

                    for (int j=0;j<chatNum;j++)
                    {
                        System.out.println(allChatTime[j]+allChat[j]);
                    }
                    String lastChatTime=allChatTime[0];
                    int last=0;
                    //遍历所有的聊天记录的数组，并找到最早的聊天记录
                    for(int j=1;j<chatNum;j++)
                    {
                        if(lastChatTime.compareTo(allChatTime[j])<0)
                        {
                            lastChatTime=allChatTime[j];
                            last=j;
                        }
                    }

                    ChatList chatList=new ChatList();
                    UserLoginForm userLoginForm=doctorChattingListService.findUserById(senderId[i]);
                    chatList.setLogoLink(userLoginForm.getLogoLink());
                    chatList.setSenderName(userLoginForm.getUserName());
                    chatList.setLastChat(allChat[last]);
                    chatList.setChatTime(lastChatTime);
                    chatList.setBoolPicture(boolPicture[last]);
                    chatList.setLastChatState(allChatState[last]);
                    chatList.setOtherTel(userLoginForm.getUserTel());
                    System.out.println(chatList.toString());
                    chatLists.add(chatList);
                }
                for(int i=0;i<receiverNum;i++)
                {
                    ChatRecord recordSender=new ChatRecord();
                    recordSender.setSenderId(doctorId);
                    recordSender.setReceiverId(receiverId[i]);
                    ArrayList<ChatRecord> records=doctorChattingListService.findChatByRecord(recordSender);
                    System.out.println("接收方："+receiverId[i]+": "+records.size());
                    String []allChat=new String[records.size()];
                    String []allChatTime=new String[records.size()];
                    Integer []boolPicture=new Integer[records.size()];
                    String []allChatState=new String[records.size()];
                    SimpleDateFormat dateTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    for(int j=0;j<records.size();j++)
                    {
                        ChatRecord record=records.get(j);
                        allChat[j]=record.getChatContent();
                        allChatTime[j]=dateTime.format(record.getChatTime());
                        boolPicture[j]= record.getFlag2();
                        allChatState[j]="已读";
                    }
                    for (int j=0;j<records.size();j++)
                    {
                        System.out.println(allChatTime[j]+allChat[j]);
                    }
                    String lastChatTime=allChatTime[0];
                    int last=0;
                    for(int j=1;j<records.size();j++)
                    {
                        if(lastChatTime.compareTo(allChatTime[j])<0)
                        {
                            lastChatTime=allChatTime[j];
                            last=j;
                        }
                    }
                    ChatList chatList=new ChatList();
                    UserLoginForm userLoginForm=doctorChattingListService.findUserById(receiverId[i]);
                    chatList.setLogoLink(userLoginForm.getLogoLink());
                    chatList.setSenderName(userLoginForm.getUserName());
                    chatList.setLastChat(allChat[last]);
                    chatList.setChatTime(lastChatTime);
                    chatList.setBoolPicture(boolPicture[last]);
                    chatList.setLastChatState(allChatState[last]);
                    chatList.setOtherTel(userLoginForm.getUserTel());
                    System.out.println(chatList.toString());
                    chatLists.add(chatList);
                }
                //按预约日期排序
                Comparator<ChatList> comparator = new Comparator<ChatList>(){
                    public int compare(ChatList s1, ChatList s2) {
                        //按照消息时间排序
                        return s2.chatTime.compareTo(s1.chatTime);
                    }
                };
                //这里就会自动根据规则进行排序
                Collections.sort(chatLists,comparator);
                return chatLists.toString();
            }
        } catch (Exception e) {
            return "False";
        }
    }
}